package com.alameda.common.aop;

import com.alameda.common.annotation.UnLogicDeleteIntercept;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 读取忽略逻辑删除aop
 *
 * @author Lv
 * @date 2024/11/24
 */
@SuppressWarnings("all")
@Aspect
@Component
public class UnLogicDeleteInterceptAop {
    /**
     * 定义线程池
     */
    public final static ThreadLocal<Boolean> FLAG_THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 定义切点
     */
    @Pointcut("@annotation(com.alameda.common.annotation.UnLogicDeleteIntercept)")
    public void pointCut() {

    }

    /**
     * 前置处理
     *
     * @param joinPoint 切点对象
     */
    @Before(value = "pointCut()")
    public void dealBefore(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        // 获取注解标注的方法
        UnLogicDeleteIntercept annotation = method.getAnnotation(UnLogicDeleteIntercept.class);
        if (annotation != null) {
            // 将逻辑删除忽略标识放入线程池
            FLAG_THREAD_LOCAL.set(Boolean.TRUE);
        }
    }
}
